﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading;

namespace HandLab.LocalApp.Learn.Threads
{
    public class ThreadInCS
    {
        bool done;
        static bool done2;

        public static void Go()
        {
            Test4();
        }

        /// <summary>
        /// 数据可以共享（通过static）
        /// </summary>
        static void Test4()
        {
            new Thread(DoneTest2).Start();
            Console.Write(1);
            DoneTest2();
            Console.Write(2);
        }


        /// <summary>
        /// 数据可以共享（当有共同引用同一对象实例时）
        /// </summary>
        static void Test3()
        {
            ThreadInCS tic = new ThreadInCS();
            new Thread(tic.DoneTest).Start();
            Console.Write(1);
            tic.DoneTest();
            Console.Write(2);
        }

         static void DoneTest2()
        {
            Console.Write(3);
            if (!done2)
            {
                done2 = true;
                //Console.Write(done);
                Console.Write("done");
            }
            Console.Write(4);
        }

        void DoneTest()
        {
            Console.Write(3);
            if (!done)
            {
                done = true;
                //Console.Write(done);
                Console.Write("done");
            }
            Console.Write(4);
        }

        static void Test2()
        {
            new Thread(WriteNum).Start();

            WriteNum();
        }

        static void WriteNum()
        {
            for (int i = 0; i < 5; i++)
                Console.Write(i);
        }

        static void Test1()
        {
            Thread thread = new Thread(WriteY);
            thread.Start();

            for (int i = 0; i < 1000; i++)
                Console.Write("x");
        }

        static void WriteY()
        {
            for (int i = 0; i < 1000; i++)
                Console.Write("y");
        }
    }
}
